<template>
	<view class="pwd-retrieve-container">
		<uni-forms ref="form" :value="order" labelWidth="80px">
			<uni-forms-item name="orderType" label="工单类型">
				<uni-data-select v-if="show" v-model="order.orderType" :localdata="orderTypeOptions" :clear="true"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item name="orderDesc" label="维修地点">
				<uni-easyinput type="textarea" v-model="order.location" placeholder="请填写维修地点" />
			</uni-forms-item>
			<uni-forms-item name="orderDesc" label="内容描述">
				<uni-easyinput type="textarea" v-model="order.orderDesc" placeholder="请详细描述您的需求,以便工作人员为您派单" />
			</uni-forms-item>
			<uni-forms-item name="bookingTime" label="预约时间">
				<view class="example-body"><uni-datetime-picker v-model="order.bookingTime" /></view>
			</uni-forms-item>
			<button type="primary" size="default" @click="submit">提交</button>
		</uni-forms>
	</view>
</template>

<script>
import { addOrder } from '@/api/business/order.js';
import { getDictByType } from '@/api/system/dict.js';

export default {
	data() {
		const currentDate = this.getDateTime({
			format: true
		});
		return {
			value: 0,
			show: true,
			// 工单状态数据字典
			orderTypeOptions: [],
			order: {
				orderType: undefined,
				orderDesc: undefined,
				bookingTime: currentDate,
				location: undefined
			},
			rules: {
				orderType: {
					rules: [
						{
							required: true,
							errorMessage: '请选择工单类型'
						}
					]
				},
				orderDesc: {
					rules: [
						{
							required: true,
							errorMessage: '请填写描述'
						}
					]
				},
				bookingTime: {
					rules: [
						{
							required: true,
							errorMessage: '请选择上门时间'
						}
					]
				},
				location: {
					rules: [
						{
							required: true,
							errorMessage: '请填写维修地点'
						}
					]
				},
			}
		};
	},
	created() {
		// console.log('option', option)
		this.getPageData();
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
		this.order.location = this.$store.state.user.address;
	},
	methods: {
		// 获取显示数据
		getPageData() {
			// 获取工单类型
			getDictByType('order_type').then(res => {
				// console.log('res', res);
				this.orderTypeOptions = res.data.map(v => {
					return {
						text: v.dictLabel,
						value: v.dictValue
					};
				});
			});
			// 获取默认维修地点
			
		},
		submit(){
			this.$refs.form.validate().then(res => {
				// let a = {
				// 	orderType: this.data.orderType,
				// 	orderDesc: this.data.orderDesc,
				// 	bookingTime: this.data.bookingTime
				// };
				// 防抖
				// this.throttle(function () {
					console.log('aa');
					addOrder(this.order).then(response => {
						setTimeout(() => {
							uni.showToast({
								title: '下单成功'
							});
							setTimeout(() => {
								uni.hideToast();
								//关闭提示后跳转
								//'/pages/work/index'
								this.$tab.navigateBack();
								// this.$tab.redirectTo('/pages/work/index')
							}, 1500);
						}, 0);
					});
				// }, 1000);
			});
		},
		getDateTime(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			let hour = date.getHours();
			let min = date.getMinutes();
			let sec = date.getSeconds();

			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;
			day = day > 9 ? day : '0' + day;
			hour = hour > 9 ? hour : '0' + hour;
			min = min > 9 ? min : '0' + min;
			sec = sec > 9 ? sec : '0' + sec;
			return `${year}-${month}-${day} ${hour}:${min}:${sec}`;
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #ffffff;
}

.pwd-retrieve-container {
	padding-top: 36rpx;
	padding: 15px;
}

button {
	margin-top: 30rpx;
	margin-bottom: 30rpx;
}

.button-sp-area {
	margin: 0 auto;
	width: 60%;
}

.mini-btn {
	margin-right: 40rpx;
}

</style>
